package com.leji.song.myOrder;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.dgl.sdk.util.JsonUtil;
import com.dgl.sdk.util.SharePreStore;
import com.dgl.sdk.view.PullListView;
import com.dgl.sdk.view.WidgetAnim;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.leji.song.R;
import com.leji.song.extra.BaseActivity;
import com.leji.song.extra.BaseCoreActivity;
import com.leji.song.extra.MyApplication;
import com.leji.song.httpInterface.BaseAjaxParams;
import com.leji.song.httpInterface.HttpInterface;
import com.leji.song.httpInterface.NomalResult;
import com.leji.song.myOrder.model.DS_MyOrder;
import com.leji.song.myOrder.model.DS_MyOrderList;
import com.leji.song.util.ActivityStackControlUtil;
import com.leji.song.util.ConstAns;
import com.leji.song.util.MyAjaxCallBack;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by colin.ding on 2014/6/4.
 */
public class MyOrderActivity extends BaseActivity{
    private PullListView orderListView;
    private TextView noData;
    private List<DS_MyOrder> myOrderList = new ArrayList<DS_MyOrder>();

    MyOrderAdapter myOrderAdapter ;

    int currentPage;

    private View commentView;
    private EditText commentEditText;
    private Button submitComment;
    private int commentPos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.my_order);

        InitView();

        getMyOrder(1);

    }

    private void InitView() {
        setActivityTitle("我的订单");

        orderListView = (PullListView) findViewById(R.id.orderListView);
        noData = (TextView) findViewById(R.id.noData);
        commentView = (View) findViewById(R.id.commentView);
        commentEditText = (EditText) findViewById(R.id.commentEditText);
        submitComment = (Button) findViewById(R.id.submitComment);

        noData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMyOrder(1);
            }
        });

        orderListView.setonRefreshListener(new PullListView.OnRefreshListener() {
            @Override
            public void onRefresh() {

                if(myOrderList.size()>0){
                    myOrderList.clear();
                }

                getMyOrder(1);
            }
        });

        submitComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(WidgetAnim.checkIfEmpty(MyOrderActivity.this, commentEditText)){
                    return;
                }

                submitComment(commentEditText.getText().toString());

                commentView.setVisibility(View.GONE);
            }
        });
    }

    private void getMyOrder(final int page) {

        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetMyOrder.params.memberid, SharePreStore.readString(ConstAns.MY_ID));
        params.put(HttpInterface.HIF_GetMyOrder.params.no, Integer.toString(page));

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_GetMyOrder.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                Object obj = NomalResult.doResult(o,MyOrderActivity.this);
                if (obj != null) {

                    try{

                        Gson gson = new Gson();

                        DS_MyOrderList orderdata = JsonUtil.fromJson(gson.toJson(obj), DS_MyOrderList.class);


                        Type listType = new TypeToken<List<DS_MyOrder>>() {
                        }.getType();
                        List<DS_MyOrder> orderList = gson.fromJson(gson.toJson(orderdata.getDatas()), listType);

                        if((orderList!= null)  && (orderList.size() >0)){

                            currentPage = page;

                            myOrderList.addAll(orderList);

                            if(myOrderAdapter == null){
                                myOrderAdapter = new MyOrderAdapter(MyOrderActivity.this, myOrderList,new MyOrderAdapter.IComment() {
                                    @Override
                                    public void onComment(int pos) {
                                        commentPos = pos;
                                        if(commentView.isShown()){
                                            commentView.setVisibility(View.GONE);
                                            InputMethodManager inputManager =

                                                    (InputMethodManager)commentEditText.getContext().getSystemService(INPUT_METHOD_SERVICE);
                                            inputManager.hideSoftInputFromWindow(commentEditText.getWindowToken(), 0);
                                        }else{
                                            commentView.setVisibility(View.VISIBLE);

                                            commentEditText.setFocusable(true);

                                            commentEditText.setFocusableInTouchMode(true);

                                            commentEditText.requestFocus();

                                            InputMethodManager inputManager =

                                                    (InputMethodManager)commentEditText.getContext().getSystemService(INPUT_METHOD_SERVICE);

                                            inputManager.showSoftInput(commentEditText, 0);
                                        }

                                    }
                                });

                                orderListView.setAdapter(myOrderAdapter);

                            }else{
                                myOrderAdapter.notifyDataSetChanged();
                            }


                            if(orderList.size()>10){
                                orderListView.setonGetMoreListener(new PullListView.OnGetMoreListener() {
                                    @Override
                                    public void onGetMore() {
                                        getMyOrder(++currentPage);
                                    }
                                });
                            }

                            orderListView.onGetMoreComplete();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }


                if(myOrderList.size()==0){
                    noData.setVisibility(View.VISIBLE);
                    orderListView.setVisibility(View.GONE);
                }else{
                    noData.setVisibility(View.GONE);
                    orderListView.setVisibility(View.VISIBLE);
                }
            }
        });
    }


    private void submitComment( String comment) {


        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_Comment.params.memberid, SharePreStore.readString(ConstAns.MY_ID));
        params.put(HttpInterface.HIF_Comment.params.comment, comment);
        params.put(HttpInterface.HIF_Comment.params.orderId, myOrderList.get(commentPos).getId());

        FinalHttp fh = new FinalHttp();
        fh.get(HttpInterface.HIF_Comment.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)this) {
            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);

                if(NomalResult.isNormalResult(o,MyOrderActivity.this)){

                    showToast("发表评论成功！");
                }
            }
        });
    }

    private long exitTime;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(commentView.isShown()){
            commentView.setVisibility(View.GONE);
            InputMethodManager inputManager =

                    (InputMethodManager)commentEditText.getContext().getSystemService(INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(commentEditText.getWindowToken(), 0);
            return true;
        }


        if (keyCode == KeyEvent.KEYCODE_BACK ) {
            if((System.currentTimeMillis() - exitTime) > 2000){
                showToast("再按一次退出掌上学苑");
                exitTime = System.currentTimeMillis();
            } else {
                ActivityStackControlUtil.finishProgram();
            }
        }

        return true;
    }
}
